//
//  SortController.m
//  RRIsSmallPig
//
//  Created by K。 on 15/7/21.
//  Copyright (c) 2015年 寇广超. All rights reserved.
//

#import "SortController.h"

@interface SortController ()

@end

@implementation SortController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"分类";
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(100 , 40 );
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 10, 10, 10);
  

    self.collectView = [[UICollectionView alloc] initWithFrame:[[UIScreen mainScreen] bounds] collectionViewLayout:flowLayout];
    self.collectView .backgroundColor = [UIColor whiteColor];
    [self.view addSubview: self.collectView ];
    self.collectView .delegate = self;
    self.collectView .dataSource = self;
    
    [self.collectView registerClass:[SortCollectionViewCell class] forCellWithReuseIdentifier:@"Reuse"];
    [self.collectView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"foot"];
    
    self.tabView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 64) style:UITableViewStylePlain ];
    [self.view addSubview:self.tabView];
    self.tabView.delegate =self;
    self.tabView.dataSource = self;
    self.tabView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.sectionNum = 0;
    self.isOpen = YES;
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.labelText = @"正在加载";
    [self.HUD show:YES];
    
    
    [self getData];
    
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.collectArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return  1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifer = @"cell";
    SortTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifer];
    if (cell == nil) {
        cell = [[SortTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifer];
    }
    cell.backgroundColor = [UIColor colorWithRed:0.4 green:0.5 blue:0.6 alpha:1.0];
    cell.sort = [self.collectArray objectAtIndex:indexPath.section];
    return cell;


}
//**注册尾
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    
    if (self.isOpen == NO) {
        
        if (self.sectionNum == section && self.isOpen == NO) {
            
            NSMutableArray *array = [self.footArray objectAtIndex:section];
            self.collectView.alpha = 0;
            [UIView animateWithDuration:0.5 animations:^{
                [self.collectView setAlpha:1.0];
                
            }];
          
            return ((array.count + 2) / 3 *51 +10);
            
        } else {
            
            return 0;
        }
    }
    return 0;

}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    
    
    return self.collectView;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.isClick == NO) {
        self.isOpen = NO;
        self.sectionNum = indexPath.section;
        [self.tabView reloadData];
        self.isClick = YES;
    }else{
        self.isOpen = YES;
        self.sectionNum = indexPath.section;
        [self.tabView reloadData];
        self.isClick = NO;
    }
    if (self.isOpen == NO) {
        NSIndexPath *indexPaths =  [NSIndexPath indexPathForRow:0 inSection:indexPath.section];
        [self.tabView scrollToRowAtIndexPath:indexPaths atScrollPosition:UITableViewScrollPositionTop animated:YES];
    }
    self.sectionNum = indexPath.section;
    [self.tabView reloadData];
    [self.collectView reloadData];
}
-(void)getData
{
    
    [AFN_block NSString:@"http://121.41.84.91:80/HandheldKitchen/api/home/tblAssort!getFirstgrade.do" Blokc:^(id request) {
        NSMutableArray *array = [request objectForKey:@"data"];
           self.collectArray = [NSMutableArray array];
        self.footArray = [NSMutableArray array];
        NSLog(@"array == %ld",array.count);
        for (NSMutableDictionary *dic in array) {
            Sort *sort = [[Sort alloc]init];
            sort.name = [dic objectForKey:@"name"];
            sort.secondgrade = [dic objectForKey:@"secondgrade"];
            sort.imagePath = [dic objectForKey:@"imagePath"];
            
            [self.collectArray addObject:sort];
            [self.footArray addObject:sort.secondgrade];
            
        }
        [self.tabView reloadData];
        [self.HUD hide:YES];
    }];
  
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    return 80;
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return  1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    NSLog(@" ***** == %ld",self.collectArray.count);
    NSMutableArray *array = [self.footArray objectAtIndex:self.sectionNum];
  
    return  array.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    SortCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Reuse" forIndexPath:indexPath];
    
    cell.nameLabel.text = [[[self.footArray objectAtIndex:self.sectionNum] objectAtIndex:indexPath.item] objectForKey:@"name"];
    NSLog(@" 11111111 %@ ", cell.nameLabel.text);
    return  cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    SecondController *secVC = [[SecondController alloc]init];
    secVC.typeId = [[[[self.footArray objectAtIndex:self.sectionNum] objectAtIndex:indexPath.item] objectForKey:@"id"] integerValue];
    secVC.navigationItem.title = [[[self.footArray objectAtIndex:self.sectionNum] objectAtIndex:indexPath.item] objectForKey:@"name"];
    secVC.isSelected = YES;
    [self.navigationController pushViewController:secVC animated:YES];

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
